/*
  Copyright 1996-2003
  Simon Whiteside

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

  $Id: HelloWorld.java,v 1.5 2003/05/06 14:26:07 simkin_cvs Exp $
*/
package simkin.examples.helloworld_midp;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import simkin.*;
import org.kxml.kdom.*;
import org.kxml.*;
import org.kxml.io.*;
import org.kxml.parser.*;
import java.io.*;

/**
 * This class shows the simplest possible application which just prints "Hello World"
 */

public class HelloWorld extends MIDlet
{
    // Create a Simkin interpreter
    Interpreter m_Interpreter=new Interpreter();
    public HelloWorld(){
    }
    /* A simple piece of XML, that just shows "Hello World" on the screen:

    <object>
    <function name="main">
    trace("Hello World");
    </function>
    </object>
    */
    static String g_XML="<object><function name=\"main\"> trace(\"Hello World\"); </function></object>";

    public void startApp(){
	Displayable current=Display.getDisplay(this).getCurrent();
	if (current==null){
	    HelloScreen screen=new HelloScreen(this);
            m_Interpreter.setTraceCallback(screen);
	    Display.getDisplay(this).setCurrent(screen);
            try{
                // Create a context
                ExecutableContext ctxt=new ExecutableContext(m_Interpreter);
                
                // create an XMLExecutable object with the xml string
                StringReader reader=new StringReader(g_XML);

                // parse into an element using the KXML parser            
                XmlParser parser=new XmlParser(reader);
                Element element=new Element();
                element.parse(parser);
                
                // create an "Executable" wrapper for the document
                XMLElementObject wrapper=new XMLElementObject("",element);
                // call the "main" method
                wrapper.method("main",null,ctxt);
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
    public void pauseApp(){
    }
    public void destroyApp(boolean b){
    }
    void exitRequested(){
        destroyApp(false);
        notifyDestroyed();
    }
}
